package com.philosophy.web.domain.generate;

import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;

import java.nio.charset.StandardCharsets;

/**
 * @author philosophy
 */
public class RoutingDB {

    private static final int MAX_DB_NUM = 16;

    /**
     * 根据字符串值获取路由的数据库的编号
     *
     * @param str str
     * @return 数据库编号
     */
    public static int getDatabaseNum(String str) {
        HashFunction hashFunction = Hashing.murmur3_128();
        HashCode hashCode = hashFunction.hashString(str, StandardCharsets.UTF_8);
        return hashCode.asInt() % MAX_DB_NUM;
    }
}
